package com.example.activemq.task;

import com.example.activemq.pojo.MessagePojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.example.activemq.config.QueueConfig;

import javax.annotation.Resource;
import javax.jms.Queue;
import java.time.LocalDateTime;
import java.util.stream.IntStream;

/**
 * @application:
 * @author: LiYuhang
 * @Date: 2021/4/3 20:01
 * @version: 0.1
 * @Description :
 * @Modified By:
 */
@Component
@EnableScheduling
@Slf4j
public class MessageProducter {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    public static final Integer ONCE_SEND_SIZE=1000;
    /**
     * @Description:   注入制定的队列 {@link QueueConfig#queueTest()}
     * @Author: LiYuhang
     * @return
     */
    @Resource(name = "queueTest")
    private Queue queueTest;

    @Resource(name = "delayQueue")
    private Queue delayQueue;


    @Scheduled(cron="0/05 * * * * ? ")
    public void messageProducter() {
        LocalDateTime now = LocalDateTime.now();
        String messageContent="现在是北京时间："+ now;
        log.info("发送消息：{}",messageContent);
        IntStream.range(0,ONCE_SEND_SIZE).forEach(v->{
            sendMsg(messageContent,now);
        });
    }


    private void sendMsg(String messageContent, LocalDateTime dateTime){
       jmsMessagingTemplate.convertAndSend(queueTest, new MessagePojo(messageContent,dateTime));

   }



}
